//
//  DataModle.m
//  LoanTeam15
//
//  Created by gynjobs on 2017/8/18.
//  Copyright © 2017年 cangming. All rights reserved.
//

#import "DataModle.h"
#import "GetJsonData.h"
static const NSDictionary * genTypeDic(){
    
    static NSDictionary * typeDic = nil;
    typeDic = @{@"text":@(DATA_TYPE_TEXT),@"date":@(DATA_TYPE_DATE),@"city":@(DATA_TYPE_CITY),
                @"num":@(DATA_TYPE_NUM),@"tel":@(DATA_TYPE_TEL),@"phone":@(DATA_TYPE_PHONE),
                @"list":@(DATA_TYPE_LIST),@"image":@(DATA_TYPE_IMAGE),@"radio":@(DATA_TYPE_RADIO),
                @"money":@(DATA_TYPE_MONEY),@"sms":@(DATA_TYPE_SMS),@"unit":@(DATA_TYPE_UNIT)
                };
    return typeDic;
}



static const  NSDictionary * genValidationDic(){
    
    static NSDictionary * validationDic = nil;
    
    validationDic = @{
                      @"money":@"fl_validationMoney:",@"username":@"fl_validationUsername:",
                      @"iden_card":@"fl_validationIdencard:",@"card_no":@"fl_validationCardnum:",
                      @"address":@"fl_validationAddress:",
                      @"school_name":@"fl_validationSchoolName:",@"grade":@"fl_validationGrade:",
                      @"company_name":@"fl_validationCompanyName:",
                      @"company_address":@"fl_validationCompanyAddress:",
                      @"salary_bank_public":@"fl_validationSalaryBankPublic:",
                      @"otherloan":@"fl_validationOtherLoan:",
                      @"salary_bank_other":@"fl_validationSalaryBankOther:",
                      @"sos_name":@"fl_validationSosName:",
                      @"sos_phone":@"fl_validationSosPhone:",
                      @"sos_name2":@"fl_validationSosName2:",@"sos_phone2":@"fl_validationSosPhone2:",
                      @"sos_address2":@"fl_validationSosAddress2:",
                      @"work_experience":@"fl_validationWorkExperience:",
                      @"credit_limit":@"fl_validationCreditLimit:"
                      };
    
    return validationDic;
}


@implementation DataModle
@synthesize key;
@synthesize name;
@synthesize type;
@synthesize required;
@synthesize tip1;
@synthesize tip2;
@synthesize tip3;
@synthesize extend;
@synthesize validationHandler;


-(instancetype)init{
    
    if(self = [super init]){
        
        key = @"";
        name = @"";
        type = 1;
        required = 0;
        tip1 = @"";
        tip2 = @"";
        tip3 = @"";
        extend = 0;
        validationHandler = nil;
        
    }
    
    return self;
}



#pragma create methods
+(id)createWithKey:(NSString *) key{
    
    
    NSDictionary * dic = [[GetJsonData standardGetInstance] getJsonBaseItemData:key];
    return [self createWithDic:dic];
    
}

+(id)createWithDic:(id)dic{
    
    DataModle* item = [[self alloc] init];
    ItemDataType dataType = [self getDataType:dic];
    
    item.key = [dic valueForKey:@"key"];
    item.name = [dic valueForKey:@"name"];
    item.type = dataType;
    item.required = [[dic valueForKey:@"required"] integerValue];
    item.tip1 = [dic valueForKey:@"tip1"];
    item.tip2 = [dic valueForKey:@"tip2"];
    item.tip3 = [dic valueForKey:@"tip3"];
    
    if([dic valueForKey:@"extend"]){
        
        item.extend = [[dic valueForKey:@"extend"] integerValue];
    }
    
    [item genValidationHandler];
    
    return item;
    
}

#pragma gen type

+(ItemDataType) getDataType:(id) dic
{
    
    const NSDictionary * typeDic = genTypeDic();
    NSString * typeKey = [dic valueForKey:@"type"];
    ItemDataType dataType = (ItemDataType)[[typeDic valueForKey:typeKey] integerValue];
    return  dataType;
    
}


#pragma gen validation handler

-(void)genValidationHandler {
    
    SEL hanlder = nil;
    const NSDictionary * validationDic = genValidationDic();
    NSString * handlerStr = [validationDic valueForKey:key];
    
    GYNLog(@"key = %@ handlerstr = %@",key,handlerStr);
    if(handlerStr){
        
        hanlder = NSSelectorFromString(handlerStr);
        self.validationHandler = hanlder;
        
        if(!hanlder){
            
            GYNLog(@"error key = %@ handlerstr = %@",key,handlerStr);
        }
        
    }else{
        
        GYNLog(@"key = %@",key);
    }
    
}



-(BOOL)validateContent:(NSString *)content withKey:(NSString *) key{
    
    
    id result;
    result = [NSString performSelector:validationHandler withObject:content];
    
    if([result integerValue]){
        
        return  YES;
        
    }else{
        
        return NO;
        
    }
    
}


@end


@implementation ItemNumData
@synthesize min;
@synthesize max;

-(instancetype)init{
    
    if(self = [super init]){
        
        min = 0;
        max = 0;
    }
    return self;
    
}


+(id)createWithDic:(id) dic{
    
    ItemNumData* item = [[self alloc] init];
    
    ItemDataType dataType = [self getDataType:dic];
    
    item.key = [dic valueForKey:@"key"];
    item.name = [dic valueForKey:@"name"];
    item.type = dataType;
    item.required = [[dic valueForKey:@"required"] integerValue];
    item.tip1 = [dic valueForKey:@"tip1"];
    item.tip2 = [dic valueForKey:@"tip2"];
    item.tip3 = [dic valueForKey:@"tip3"];
    item.min = [[dic valueForKey:@"min"] integerValue];
    item.max = [[dic valueForKey:@"max"] integerValue];
    
    if([dic valueForKey:@"extend"]){
        
        item.extend = [[dic valueForKey:@"extend"] integerValue];
    }
    
    [item genValidationHandler];
    
    return item;
}


-(BOOL)validateContent:(NSString *)content withKey:(NSString *) key{
    
    
    id result;
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionaryWithCapacity:3];
    [dic setValue:content forKey:@"content"];
    [dic setValue:@(self.min) forKey:@"min"];
    [dic setValue:@(self.max) forKey:@"max"];
    
    result = [NSString performSelector:validationHandler withObject:dic];
    
    if([result integerValue]){
        
        return  YES;
        
    }else{
        
        return NO;
        
    }
    
}



@end



@implementation ItemListData
@synthesize datasource;

-(instancetype)init{
    
    if(self = [super init]){
        
        datasource = @"";
        
    }
    return self;
}

+(id)createWithDic:(id) dic{
    
    ItemListData* item = [[self alloc] init];
    
    ItemDataType dataType = [self getDataType:dic];
    
    item.key = [dic valueForKey:@"key"];
    item.name = [dic valueForKey:@"name"];
    item.type = dataType;
    item.required = [[dic valueForKey:@"required"] integerValue];
    item.tip1 = [dic valueForKey:@"tip1"];
    item.tip2 = [dic valueForKey:@"tip2"];
    item.tip3 = [dic valueForKey:@"tip3"];
    item.datasource = [dic valueForKey:@"datasource"];
    
    if([dic valueForKey:@"extend"]){
        
        item.extend = [[dic valueForKey:@"extend"] integerValue];
    }
    
    // item.extend = 1;
    return item;
}


@end



@implementation ItemDataFactory

+(DataModle*)create:(NSString*)key{
    
    // type
    NSDictionary * dic = [[GetJsonData standardGetInstance] getJsonBaseItemData:key];
    return [self createWithDic:dic];
    
}


+(DataModle*)createWithDic:(NSDictionary*)dic{
    
    ItemDataType dataType = [DataModle getDataType:dic];
    
    // item
    DataModle * itemData = nil;
    
    switch (dataType) {
            
        case DATA_TYPE_TEXT:
        case DATA_TYPE_PHONE:
        case DATA_TYPE_TEL:
        case DATA_TYPE_RADIO:
        case DATA_TYPE_SMS:
        {
            itemData = [DataModle createWithDic:dic];
        }
            break;
        case DATA_TYPE_NUM:
        case DATA_TYPE_UNIT:
        {
            itemData = [ItemNumData createWithDic:dic];
            
        }
            break;
        case DATA_TYPE_CITY:
        case DATA_TYPE_LIST:
        {
            itemData = [ItemListData createWithDic:dic];
            
        }
            
            break;
        case DATA_TYPE_IMAGE:
        {
            
            itemData = [ItemListData createWithDic:dic];
            
        }
            break;
        case DATA_TYPE_DATE:
        {
            itemData = [ItemListData createWithDic:dic];
        }
            
            break;
            
        default:
            break;
    }
    
    return itemData;
    
    
}

@end

